<template>
  <el-dialog
    @close="reset"
    :title="menuData.id > 0 ? '编辑' : '新增'"
    :visible.sync="visible"
  >
    <el-form ref="menuForm" :rules="rules" :model="menuData" label-width="80px">
      <el-form-item label="上级菜单" prop="pid">
        <el-select placeholder="请选择" v-model="menuData.pid">
          <el-option label="顶级菜单" :value="0"> </el-option>
          <el-option
            v-for="item in menuList"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="菜单类型" prop="type">
        <el-radio-group v-model="menuData.type">
          <el-radio :label="1">一级</el-radio>
          <el-radio :label="2">二级</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="菜单名称" prop="title">
        <el-input v-model="menuData.title" placeholder="菜单名称"></el-input>
      </el-form-item>

      <!-- 实现条件渲染 -->
      <el-form-item label="菜单地址" prop="url">
        <el-input v-model="menuData.url" placeholder="菜单地址"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="reset">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  computed: {
    ...mapState(["menuList"]),
  },
  data() {
    const validPid = (role, value, callback) => {
      // 上级菜单不能选择自己
      if (this.menuData.id === value) {
        return callback(new Error("上级菜单不能选择自己"));
      }
      callback();
    };
    // 自定义表单校验函数
    const validType = (rule, value, callback) => {
      // 表示用户选择的菜单类型为目录
      if (this.menuData.type === 1) {
        if (this.menuData.pid !== 0) {
          return callback(new Error("请选择顶级菜单作为上级菜单"));
        }
      }
      // 表示用户选择的菜单类型为菜单
      if (this.menuData.type === 2) {
        if (this.menuData.pid === 0) {
          return callback(new Error("请选择一个一级菜单作为上级菜单"));
        }
      }
      // 表示表单校验通过
      callback();
    };
    return {
      // 控制对话框显示状态的数据
      visible: false,
      // 表单数据
      menuData: {
        pid: "",
        title: "",
        type: "",
        url: "",
      },
      //  表单校验
      rules: {
        pid: [
          { required: true, message: "请选择上级菜单", trigger: "blur" },
          { validator: validPid, trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择菜单类型", trigger: "blur" },
          { validator: validType },
        ],
        title: { required: true, message: "请输入菜单内容", trigger: "blur" },
        url: { required: true, message: "请输入菜单地址", trigger: "blur" },
      },
    };
  },
  methods: {
    ...mapMutations(["menuAdd", "menuUpdate"]),
    // 获取待编辑的菜单数据
    // menuData: 待编辑的带单数据, 有调用者传递
    getMenuData(menuData) {
      // 1-更新表单数据对象
      this.menuData = { ...menuData };
    },
    // 数据重置
    reset() {
      // 关闭对话框
      this.visible = false;
      // 重置表单数据
      this.menuData = {
        pid: "",
        title: "",
        url: "",
        type: "",
      };
      // 移除表单校验信息
      this.$refs.menuForm.resetFields();
    },
    submit() {
      this.$refs.menuForm.validate((valid) => {
        if (valid) {
          // 根据pid确定菜单类型
          this.menuData.type = this.menuData.pid == 0 ? 1 : 2;
          if (this.menuData.id > 0) {
            // 编辑
            this.menuUpdate(this.menuData);
          } else {
            // 新增
            this.menuAdd({ ...this.menuData });
          }
          // 重置数据
          this.reset();
        }
      });
    },
  },
};
</script>

<style>
</style>